
#include "HeaderFiles.h"


STR_LED_CTRL ledctrlHdl;


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void led_status_anlysis(STR_LED_CTRL *ledhdl, uint8_t status)
{
	
	ledhdl->status_old = ledhdl->status;
	
	switch(status)
	{
		case 0x01:
			
			ledhdl->status = 1 << LED_OFF;
			ledhdl->time_red_on    = 0;
			ledhdl->time_red_off   = 0;
			ledhdl->time_white_on  = 0;
			ledhdl->time_white_off = 0;
			ledhdl->number_cyc     = 65535;
		
			break;
		
		case 0x02:
	
			ledhdl->status = 1 << POWER_ON;
			ledhdl->time_red_on    = 0;
			ledhdl->time_red_off   = 0;
			ledhdl->time_white_on  = 10;
			ledhdl->time_white_off = 0;
			ledhdl->number_cyc     = 65535;
		
			break;
		
		case 0x04:
			
			ledhdl->status = 1 << NETWORK_OFFLINE;
			ledhdl->time_red_on    = 10;
			ledhdl->time_red_off   = 10;
			ledhdl->time_white_on  = 10;
			ledhdl->time_white_off = 10;
			ledhdl->number_cyc     = 65535;
		
			break;
		
		case 0x08:
			
			ledhdl->status = 1 << NETWORK_ONLINE;
			ledhdl->time_red_on    = 0;
			ledhdl->time_red_off   = 0;
			ledhdl->time_white_on  = 25;
			ledhdl->time_white_off = 25;
			ledhdl->number_cyc     = 3;		
			
			break;
		
		case 0x10:
			
			ledhdl->status = 1 << BATTERY_LOW;
			ledhdl->time_red_on    = 10;
			ledhdl->time_red_off   = 10;
			ledhdl->time_white_on  = 0;
			ledhdl->time_white_off = 0;
			ledhdl->number_cyc     = 65535;		
		
			break;
		
		case 0x20:
			
			ledhdl->status = 1 << CHARGING;
			ledhdl->time_red_on    = 25;
			ledhdl->time_red_off   = 50;
			ledhdl->time_white_on  = 0;
			ledhdl->time_white_off = 0;
			ledhdl->number_cyc     = 65535;		
		
			break;
		
		case 0x40:
			
			ledhdl->status = 1 << CHARGE_COMPLETE;
			ledhdl->time_red_on    = 0;
			ledhdl->time_red_off   = 0;
			ledhdl->time_white_on  = 0;
			ledhdl->time_white_off = 0;
			ledhdl->number_cyc     = 65535;
		
			break;
		
		case 0x80:
			
			ledhdl->status = 1 << FAULT;
			ledhdl->time_red_on    = 25;
			ledhdl->time_red_off   = 25;
			ledhdl->time_white_on  = 0;
			ledhdl->time_white_off = 0;
			ledhdl->number_cyc     = 3;		
			break;
		
		case 0xFF:
			
			ledhdl->status = LED_DEBUG;
			ledhdl->time_red_on    = 10;
			ledhdl->time_red_off   = 10;
			ledhdl->time_white_on  = 10;
			ledhdl->time_white_off = 10;
			ledhdl->number_cyc     = 65535;		
			break;
		
		
		default:
			
			ledhdl->status = 1 << LED_OFF;
			ledhdl->time_red_on    = 0;
			ledhdl->time_red_off   = 0;
			ledhdl->time_white_on  = 0;
			ledhdl->time_white_off = 0;
			ledhdl->number_cyc     = 65535;
		
			break;
	}
}


/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void led_red_ctrl(STR_LED_CTRL *ledhdl)
{
	uint16_t time_x1 = ledhdl->time_red_on;
	uint16_t time_x2 = time_x1 + ledhdl->time_red_off;
	uint16_t time_x3 = time_x2 + ledhdl->time_white_on;
	uint16_t time_x4 = time_x3 + ledhdl->time_white_off;
	
	ledhdl->blankcnt++;
	
	if(ledhdl->cyc_cnt < ledhdl->number_cyc)
	{
		// LED red ctrl 
		if(ledhdl->blankcnt < time_x1)
		{
			LED_RED_ON();
		}
		else if(ledhdl->blankcnt < time_x2)
		{
			LED_RED_OFF();
		}
		
		// LED white ctrl
		else if(ledhdl->blankcnt < time_x3)
		{
			LED_WHITE_ON();
		}
		else if(ledhdl->blankcnt < time_x4)
		{
			LED_WHITE_OFF();
		}
		
		// cyc or reset
		else
		{
			ledhdl->cyc_cnt++;
			ledhdl->blankcnt = 0;
		}		
	}
	
	else
	{
		LED_RED_OFF();
		LED_WHITE_OFF();
	}
}




/*!
    \brief      
    \param[in]  none
    \param[out] none
    \retval     none
*/
void led_status_ctrl(uint8_t status)
{
	
	if(ledctrlHdl.status != status)
	{
		ledctrlHdl.cyc_cnt = 0;
		ledctrlHdl.blankcnt = 0;
		
		LED_RED_OFF();
		LED_WHITE_OFF();
		
		led_status_anlysis(&ledctrlHdl, status);
	}
		
	led_red_ctrl(&ledctrlHdl);
}


